﻿using A.M.ETO;
using A.M.Storage.Infrastruecure;
using L.M.System.Domain.Entity;
using Newtonsoft.Json;
using Public_Class_Library;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using W.M.Materiel.Domain.Entity;
using W.M.Materiel.Infrastruecure.DTO;
using W.M.Materiel.Infrastruecure.IRepository;

namespace W.M.Materiel.Infrastruecure.Repository
{
    internal class Product_management_tableRepository : BaseRepository<product_management_table, string>, IProduct_management_tableRepository
    {
        private readonly ISqlSugarClient _client;
        private readonly Cross_service_Helper cross_Service_Helper;

        public Product_management_tableRepository(ISqlSugarClient client, Cross_service_Helper cross_Service_Helper) : base(client)
        {
            _client = client;
            this.cross_Service_Helper = cross_Service_Helper;
        }

        /// <summary>
        /// 产品信息+分页+查询
        /// </summary>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="name">查询条件:产品名称</param>
        /// <param name="id">查询条件:单位id</param>
        /// <returns>产品信息</returns>
        public List<Product_managementDTO> GetProductionunit(int pageSize, int pageIndex, string? name, string? id, out int totalCount, out int pageCount)
        {
            var obj = cross_Service_Helper.Get("http://10.31.59.5:8001/api/Table_of_unitsRead/GetTableUrl").Result;
            var jsonObj = JsonConvert.DeserializeObject<List<table_of_units>>(obj);

            var plist = _client.Queryable<product_management_table>().ToList();


            var list = (from a in jsonObj
                        join b in plist
                        on a.unit_number equals b.unit_id
                        where (id == null || b.unit_id == id) &&
                        (string.IsNullOrEmpty(name) || b.product_name.Contains(name))
                        select new Product_managementDTO
                        {
                            #region
                            //unit_id = a.unit_number,
                            //attachments = b.attachments,
                            //auxiliary_unit = b.auxiliary_unit,
                            //carton_size = b.carton_size,
                            //product_attribute_id = b.product_attribute_id,
                            //product_classification_id = b.product_classification_id,
                            //product_expiration_date = b.product_expiration_date,
                            //product_name = b.product_name,
                            //product_number = b.product_number,
                            //remark = b.remark,
                            //selling_number = b.selling_number,
                            //specification_model = b.specification_model,
                            //status = b.status,
                            //unit_name = a.unit_name,
                            //unit_sales_price = b.unit_sales_price,
                            #endregion
                            unit_id = a.unit_number,
                            alarm_days = b.alarm_days,
                            attachments = b.attachments,
                            auxiliary_unit = b.auxiliary_unit,
                            brand = b.brand,
                            carton_size = b.carton_size,
                            maximum_inventory = b.maximum_inventory,
                            minimum_inventory = b.minimum_inventory,
                            packing_method = b.packing_method,
                            packing_requirement = b.packing_requirement,
                            picture = b.picture,
                            product_attribute_id = b.product_attribute_id,
                            product_classification_id = b.product_classification_id,
                            product_expiration_date = b.product_expiration_date,
                            product_name = b.product_name,
                            product_number = b.product_number,
                            remark = b.remark,
                            selling_number = b.selling_number,
                            specification_model = b.specification_model,
                            status = b.status,
                            unit_name=a.unit_name,
                            unit_sales_price = b.unit_sales_price,
                        }).ToList();
            #region
            //var list = (from a in jsonObj
            //            join b in _client.Queryable<product_management_table>().ToList()
            //            on a.unit_number equals b.unit_id
            //            where (id == null || b.unit_id == id) &&
            //            (string.IsNullOrEmpty(name) || b.product_name.Contains(name))
            //            select new Product_managementDTO
            //            {
            //                unit_id = a.unit_number,
            //                alarm_days = b.alarm_days,
            //                attachments = b.attachments,
            //                auxiliary_unit = b.auxiliary_unit,
            //                brand = b.brand,
            //                carton_size = b.carton_size,
            //                maximum_inventory = b.maximum_inventory,
            //                minimum_inventory = b.minimum_inventory,
            //                packing_method = b.packing_method,
            //                packing_requirement = b.packing_requirement,
            //                picture = b.picture,
            //                product_attribute_id = b.product_attribute_id,
            //                product_classification_id = b.product_classification_id,
            //                product_expiration_date = b.product_expiration_date,
            //                product_name = b.product_name,
            //                product_number = b.product_number,
            //                remark = b.remark,
            //                selling_number = b.selling_number,
            //                specification_model = b.specification_model,
            //                status = b.status,
            //                unit_sales_price = b.unit_sales_price,
            //            }).ToList();
            #endregion
             totalCount = list.Count();
             pageCount = (int)Math.Ceiling(totalCount * 1.0 / pageSize);
            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

            return list;
        }

    }
}
